package com.sl.hx.shop;


import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.SplashBean;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.constant.SPUKey;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.classify.ClassifyFragment;
import com.sl.hx.shop.ui.collect.CollectFragment;
import com.sl.hx.shop.ui.home.HomeFragment;
import com.sl.hx.shop.ui.home.activity.SlectStoreActivity;
import com.sl.hx.shop.ui.home.activity.WebAvtivity;
import com.sl.hx.shop.ui.mine.MineFragment;
import com.sl.hx.shop.utils.SPUtils;
import com.sl.hx.shop.utils.ToastUtil;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;

public class MainActivity extends BaseActivity {


    @BindView(R.id.id_content)
    FrameLayout idContent;
    @BindView(R.id.btn_home)
    ImageButton btnHome;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.Home)
    LinearLayout Home;
    @BindView(R.id.btn_classify)
    ImageButton btnClassify;
    @BindView(R.id.tv_classify)
    TextView tvClassify;
    @BindView(R.id.Classify)
    LinearLayout Classify;
    @BindView(R.id.btn_collect)
    ImageButton btnCollect;
    @BindView(R.id.tv_collect)
    TextView tvCollect;
    @BindView(R.id.Collect)
    public ViewGroup Collect;
    @BindView(R.id.btn_mine)
    ImageButton btnMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    @BindView(R.id.mine)
    LinearLayout mine;
    private FragmentManager fragmentManager;
    private ClassifyFragment classifyFragment;
    private CollectFragment collectFragment;
    private HomeFragment homeFragment;
    private MineFragment mineFragment;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        fragmentManager = getSupportFragmentManager();
        setTabSelection(0);

    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.Home, R.id.Classify, R.id.Collect, R.id.mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Home:
                setTabSelection(0);
                buttonCount(1);
                break;
            case R.id.Classify:
                setTabSelection(1);
                buttonCount(2);
                break;
            case R.id.Collect:
                setTabSelection(2);
                buttonCount(3);
                break;
            case R.id.mine:
                setTabSelection(3);
                buttonCount(4);
                break;
            default:
                //StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 0);
                break;
        }
    }


    /**
     * 根据传入的index参数来设置选中的tab页。
     */
    public void setTabSelection(int index) {
        // 重置按钮
        resetBtn();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0://首页
                // 当点击了消息tab时，改变控件的图片和文字颜色
                btnHome.setImageResource(R.drawable.menu_home_selected);
                tvHome.setTextColor(getResources().getColor(R.color.main_color));
                if (homeFragment == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.id_content, homeFragment);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(homeFragment);
                }
                break;
            case 1://分类
                // 当点击了消息tab时，改变控件的图片和文字颜色
                btnClassify.setImageResource(R.drawable.menu_classify_selected);
                tvClassify.setTextColor(getResources().getColor(R.color.main_color));
                if (classifyFragment == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    classifyFragment = new ClassifyFragment();
                    transaction.add(R.id.id_content, classifyFragment);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(classifyFragment);
                }
                break;
            case 2://收藏
                btnCollect.setImageResource(R.drawable.menu_collect_selected);
                tvCollect.setTextColor(getResources().getColor(R.color.main_color));
                if (collectFragment == null) {
                    collectFragment = new CollectFragment();
                    transaction.add(R.id.id_content, collectFragment);
                } else {
                    transaction.show(collectFragment);
                }
                break;
            case 3://我的
                btnMine.setImageResource(R.drawable.menu_mine_selected);
                tvMine.setTextColor(getResources().getColor(R.color.main_color));
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.id_content, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void resetBtn() {
        tvHome.setTextColor(getResources().getColor(R.color.menutext));
        tvClassify.setTextColor(getResources().getColor(R.color.menutext));
        tvCollect.setTextColor(getResources().getColor(R.color.menutext));
        tvMine.setTextColor(getResources().getColor(R.color.menutext));
        btnHome.setImageResource(R.drawable.menu_home_normal);
        btnClassify.setImageResource(R.drawable.menu_classify_normal);
        btnCollect.setImageResource(R.drawable.menu_collect_normal);
        btnMine.setImageResource(R.drawable.menu_mine_normal);
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     * 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (classifyFragment != null) {
            transaction.hide(classifyFragment);
        }
        if (collectFragment != null) {
            transaction.hide(collectFragment);
        }
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }

    }

    private void buttonCount(int type) {
        Observable observable = ApiFactory.getInstance().buttonCount(type, SPUtils.getInstance().getInt(SPUKey.TOKENID), AppConfig.CHANNEL);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, this, ActivityEvent.DESTROY);
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }


    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private long mFirstTime = 0;

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - mFirstTime > 2000) {
            ToastUtil.showShort(this, "再按一次退出");
            mFirstTime = secondTime;
        } else {
            System.exit(0);
        }
    }

    //activity加载完成调用
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (homeFragment instanceof IOnFocusListenable) {
            ((IOnFocusListenable) homeFragment).onWindowFocusChanged(hasFocus);
        }
    }

    public interface IOnFocusListenable {
        public void onWindowFocusChanged(boolean hasFocus);
    }


}
